<template>
  <a-form :rules="memoRules" :model="memoFormConfig.form" @finish="submit" :label-col="{ span: 6 }"
    :wrapper-col="{ offset: 1 }" label-align="right" ref="memoFormRef" :disabled="hasPerm('ax:memo:edit')">
    <FormCard title="编辑/新增" :back="back" subtitle="备忘录">
      <template #extra>
        <a-button htmlType="submit" type="primary" :loading="memoTable.loading" block>
          保存
        </a-button>
      </template>
      <a-row :gutter="8">
        <a-col :span="16">
          <a-card>
            <a-form-item label="备忘录标题" name="title">
              <a-input allow-clear placeholder="请输入备忘录标题" v-model:value="memoFormConfig.form.title">
              </a-input>
            </a-form-item>
            <a-form-item label="备忘录的值" name="value">
              <a-input allow-clear placeholder="请输入备忘录的值" v-model:value="memoFormConfig.form.value">
              </a-input>
            </a-form-item>
            <a-form-item label="备忘录描述" name="description">
              <a-input allow-clear placeholder="请输入备忘录描述" v-model:value="memoFormConfig.form.description">
              </a-input>
            </a-form-item>
            <a-form-item label="附加值" name="extra">
              <a-input allow-clear placeholder="请输入附加值" v-model:value="memoFormConfig.form.extra">
              </a-input>
            </a-form-item>
            <a-form-item label="用户ID" name="userId">
              <a-input allow-clear placeholder="请输入用户ID" v-model:value="memoFormConfig.form.userId">
              </a-input>
            </a-form-item>
            <a-form-item label="1：备忘录，2：记事本" name="type">
              <a-input allow-clear placeholder="请输入1：备忘录，2：记事本" v-model:value="memoFormConfig.form.type">
              </a-input>
            </a-form-item>
          </a-card>
        </a-col>
        <AxExtra></AxExtra>
      </a-row>
    </FormCard>
  </a-form>
</template>

<script setup lang="ts">
import {
} from '../data/options';
import { hasPerm } from '@/utils/common/perm';
import type { Response } from '@/api/config/types';
import {
  createMemo,
  updateMemo,
} from '@/api/modules/ax/memo/memo';
import { notify, } from '@/utils/table/table';
import { memoList } from '../data/curd';
import {
  memoFormConfig,
  memoRules,
  memoFormRef
} from '../data/form';
import { memoTable } from '../data/table';
import AxExtra from './AxExtra.vue';

const submit = async () => {
  memoTable.value.loading = true;
  let result: undefined | Response<any> = undefined
  if (memoFormConfig.value.form.memoId) {
    const { data } = await updateMemo(memoFormConfig.value.form)
    result = data
  } else {
    const { data } = await createMemo(memoFormConfig.value.form)
    result = data
  }
  notify(result)
  memoTable.value.loading = false;
  await memoList();
  back();
};

const back = () => {
  memoFormConfig.value.display = false;
  memoFormConfig.value.reset!();
};

</script>

<style lang="scss" scoped></style>
